package controller.Supper;

import com.alibaba.fastjson.JSON;
import pojo.Customer;
import service.Supper.SuperCustomerService;
import service.Supper.impl.SuperCustomerServiceImpl;
import util.PageSupport;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;

/**
 * 查询顾客Servlet
 */
@WebServlet(name = "CustomerServlet",urlPatterns = "/doCus")
public class CustomerServlet extends HttpServlet {
    private String id;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        SuperCustomerService scs = new SuperCustomerServiceImpl();
        PrintWriter out = response.getWriter();

        String action = request.getParameter("action");
        if(action ==null){
            action ="cusQueryLike";
        }
        int pageSize=5;
        if("cusQueryLike".equals(action)){   //过滤查询
            PageSupport pageSupport = new PageSupport();
            String ids = request.getParameter("id");
            if(ids==null || ids==""){
                ids="-1";
            }
            Long id = Long.valueOf(ids);
            String cusName = request.getParameter("cusName");
            String cusSex = request.getParameter("cusSex");
            int currentPage = Integer.valueOf(request.getParameter("currentPage"));   //当前页码
            int totalCount = scs.getTotalCount(id,cusName,cusSex);   //得到总数据行
            pageSupport.setCurrentPage(currentPage);
            pageSupport.setPageSize(pageSize);
            pageSupport.setTotalCount(totalCount);
            pageSupport.setTotalPages(pageSupport.getTotalPages());
            List<Customer> newsList = scs.selCustomer(currentPage,pageSize,id,cusName,cusSex);
            pageSupport.setList(newsList);
            String jsonList=JSON.toJSONStringWithDateFormat(pageSupport,"yyyy-MM-dd");
            out.print(jsonList);
        }else if("selByID".equals(action)){   //根据id进行查询
            String id = request.getParameter("id");
            List<Customer> list = scs.selCusByID(Long.valueOf(id));
            String newsListJson = JSON.toJSONStringWithDateFormat(list,"yyyy-MM-dd");
            out.write(newsListJson);
        }else if("cusUpdate".equals(action)){   //修改
            PageSupport pageSupport = (PageSupport) request.getSession().getAttribute("Page");
            Customer cus = new Customer();
            cus.setId((long)Integer.valueOf(request.getParameter("id")));
            cus.setCusName(request.getParameter("cusName"));
            cus.setCusLoginName(request.getParameter("cusLoginName"));
            cus.setCusEmail(request.getParameter("cusEmail"));
            cus.setCusSex(request.getParameter("cusSex"));
            cus.setCusHobby(request.getParameter("cusHobby"));
            cus.setCusCode(request.getParameter("cusCode"));
            cus.setCusBirthday(Date.valueOf(request.getParameter("cusBirthday")));
            int n = scs.updateScus(cus);
            boolean flag=false;
            if(n>0){
                flag=true;
            }
            String result="{\"flag\":\""+flag+"\"}";
            out.write(result);
        }else if("delSercus".equals(action)){   //删除
            Long id = Long.valueOf(request.getParameter("id"));
            int n = scs.delScus(id);
            boolean flag = false;
            if(n>0){
                flag = true;
            }
            String result="{\"flag\":\""+flag+"\"}";
            out.write(result);
        }

        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
